package com.ITArmy.controller;

import com.ITArmy.common.ServerResponse;
import com.ITArmy.controller.base.BaseController;
import com.ITArmy.dao.OrderMapper;
import com.ITArmy.util.GetUserSession;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//添加章节信息

@Api(value = "章节层次", tags = "章节接口")
@Controller
@RequestMapping("/cahpter")
public class ChapterController extends BaseController {
    GetUserSession getUserSession = new GetUserSession();
    @Autowired
    OrderMapper orderMapper;

    @RequestMapping("/insertChapter")
    @ResponseBody
    @ApiOperation(value = "添加章节，",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse insertChapter(String ch_title, int ch_number, String ch_address, long sId) {
        return chapterService.insertSerialize(request, ch_title, ch_number, ch_address, sId);
    }

    @RequestMapping("/getChapter")
    @ApiOperation(value = "获取当前连载的所有章节",
            httpMethod = "POST", response = String.class)
    public String getChapter(long sId, Model model) {
        model.addAttribute("chapter", chapterService.getAllChapter(sId).getData());
        model.addAttribute("sId", sId);
        return "bookChapter";
    }

    @RequestMapping("/ReadSerializeChapter")
    @ApiOperation(value = "进行阅读页面",
            httpMethod = "GET", response = String.class)
    public String ReadSerializeChapter(long sId, Model model) {
        //章节信息
        model.addAttribute("chapter", chapterService.getAllChapter(sId).getData());
        //书的具体信息
        model.addAttribute("serialize", serializeService.findAllSerializeById(sId).getData());
        //用户是否买过这本书
        boolean isBuy = false;
        if (orderMapper.isBy(getUserSession.getUser(request).getuId(), sId) != null) {
            isBuy = true;
        }
        model.addAttribute("isBuy", isBuy);
        return "read";
    }

    @RequestMapping("/writeChapyerPage")
    @ApiOperation(value = "进行写新章节的页面", httpMethod = "POST", response = ServerResponse.class)
    public String writeChapyer(Model model, long sId) {
        model.addAttribute("sId", sId);
        return "Serialization";
    }

}
